package com.pe1n.yangtaopai.page.search.view.activity;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.MySearchAdapter;
import com.pe1n.yangtaopai.bean.HotBean;
import com.pe1n.yangtaopai.bean.SearchBean;
import com.pe1n.yangtaopai.custom.FlowLayout;
import com.pe1n.yangtaopai.page.search.presenter.PresenterImpl;
import com.pe1n.yangtaopai.page.search.view.interfacese.IView;
import com.pe1n.yangtaopai.utils.MyConstant;
import com.pe1n.yangtaopai.utils.URLConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class SearchActivity extends AppCompatActivity implements IView {

    private Context mContext = this;
    private List<String> mList = new ArrayList<>();
    private List<SearchBean.DataBean.ProdlistBean> mSeachDataList = new ArrayList<>();
    private PresenterImpl mPresenter;
    private FlowLayout flowLayout;
    private ImageView imageViewBack;
    private TextView textViewSearch;
    private EditText editText;
    private MySearchAdapter adapter;
    private XRecyclerView xRecyclerView;
    private String url;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mPresenter = new PresenterImpl(this);
        initData();
        initView();

    }

    private void initView() {

        xRecyclerView = (XRecyclerView) findViewById(R.id.xrecview_search);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPresenter.loadSearchBeanData(url);
            }

            @Override
            public void onLoadMore() {
                page++;
                url = String.format(url, page);
                mPresenter.loadSearchBeanData(url);
            }
        });
        flowLayout = (FlowLayout) findViewById(R.id.float_layout);
        imageViewBack = (ImageView) findViewById(R.id.iv_back_search);
        imageViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        editText = (EditText) findViewById(R.id.et_search);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (i2 == 0) {
                    if (xRecyclerView.getVisibility() == View.VISIBLE) {
                        xRecyclerView.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        textViewSearch = (TextView) findViewById(R.id.tv_search_btn);
        textViewSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                page = 1;
                if (xRecyclerView.getVisibility() == View.GONE) {
                    xRecyclerView.setVisibility(View.VISIBLE);
                }
                mSeachDataList.clear();
                url = String.format(URLConstant.SEARCH_URL, editText.getEditableText() + "", page);
                if (editText.getEditableText() != null) {
                    mPresenter.loadSearchBeanData(url);
                }
            }
        });

    }

    private void initData() {

        mPresenter.loadHotBeanData(URLConstant.HOT_URL);

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

        xRecyclerView.refreshComplete();
        xRecyclerView.loadMoreComplete();

    }

    @Override
    public void showLoadFailMsg() {

    }

    @Override
    public void loadHotBean(HotBean hotBean) {
        mList = hotBean.getData();
        loadData();
    }

    @Override
    public void loadSearchBean(SearchBean searchBean) {
        List<SearchBean.DataBean.ProdlistBean> list = searchBean.getData().getProdlist();
        mSeachDataList.addAll(list);
        adapter = new MySearchAdapter(mContext, mSeachDataList);
        xRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, OrientationHelper.VERTICAL, false));
        xRecyclerView.setAdapter(adapter);
        //adapter.notifyDataSetChanged();
    }

    private void loadData() {

        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = 20;
        lp.rightMargin = 20;
        lp.topMargin = 20;
        lp.bottomMargin = 20;
        for (int i = 0; i < mList.size(); i++) {
            final int j = i;
            TextView view = new TextView(this);
            view.setText(mList.get(i));
            view.setTextColor(Color.WHITE);
            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.text_bac));
            flowLayout.addView(view, lp);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    page = 1;
                    mSeachDataList.clear();
                    url = String.format(URLConstant.SEARCH_URL, mList.get(j), page);
                    mPresenter.loadSearchBeanData(url);
                    if (xRecyclerView.getVisibility() == View.GONE) {
                        xRecyclerView.setVisibility(View.VISIBLE);
                    }
                    editText.setText(mList.get(j));
                }
            });
        }

    }
}
